<?php

//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                                  |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                     |
// +------------------------------------------------------------------------+
// | Author: anguolei@clcw.com.cn                                           |
// +------------------------------------------------------------------------+
//
/**
 * 车辆定价
 */

namespace AuctionBack\Controller;

use AuctionCore\Account\Back\Employee as Employee;
use AuctionCore\CarResource\Car as Car;
use AuctionCore\CarResource\CarList;

class CarPricingController extends BaseController
{

    public function __construct()
    {
        parent::__construct();
        $this->emp_id = $this->user_info['emp_id'];
    }

    public function index()
    {
        $this->display();
    }

    //定价列表
    public function priceList()
    {
        $page = I("get.p/d", 1);
        $size = 10;
        $paramer = array();
        $paramer['is_priced'] = I('get.is_priced');
        $paramer['keyword'] = I('get.car_no');
        $paramer['checker_id'] = I("get.checker_id");
        $paramer['price_id'] = I("get.price_id");
        $paramer['location_area'] = I('get.location_area');
        if (!$paramer['location_area']) {
            $paramer['location_area'] = !$this->is_hq ? array_flip($this->bc_city) : array();
        }
        $Car = new CarList();
        $result = $Car->getCarAndOwnerList($page, $size, $paramer);
        $total = empty($result['total']) ? 0 : $result['total'];
        $list = empty($result['list']) ? array() : $result['list'];
        //分页
        $Page = new \AuctionCore\Common\AjaxPage($total, $size, "get_page");
        $Page->setConfig('theme', " %totalRow% %header% %nowPage%/%totalPage% 页 %first% %upPage% %prePage% %linkPage% %downPage% %nextPage% %end% %jumpform%");
        $show = $Page->show(); // 分页显示输出
        //获取评估师信息
        $checkers = \AuctionCore\Account\Back\Employee::get_branch_emp(C('APPRAISER.0'), $this->user_info['branch_id']);
        //获取定价人信息
        $pricer = \AuctionCore\Account\Back\Employee::get_branch_emp(C('PRICING_DIVISION.0'), $this->user_info['branch_id']);
        $this->assign("checker", $checkers);
        $this->assign('pricer', $pricer);
        $this->assign('page', $show); // 赋值分页输出        
        $this->assign('list', $list);
        $this->assign('p', $page);
        $this->assign('checkerid', $paramer['checker_id']);
        $this->assign('pricer_id', $paramer['price_id']);
        $this->assign('is_priced', $paramer['is_priced']);
        $this->assign('location_area', $paramer['location_area']);
        $this->assign('car_no', $paramer['keyword']);
        $this->display();
    }

    //加载定价页面
    public function price()
    {
        $car_id = I("get.car_id/d");
        $is_priced = I('get.is_priced/d');
        $p = I('get.p/d');
        if ((int)$car_id < 1) {
            $this->helpJsRedirect("车辆信息有误");
        }
        $c = new Car($car_id);
        $carInfo = $c->detail();
        $carIds = $c->getCarIdsBySid($carInfo['sid']);
        $count = count($carIds);

        $carInfo['transfer_limit'] = C('TRANSFER_LIMIT.' . $carInfo['transfer_limit']);
        $carInfo['suggest_price'] = format_money($carInfo['suggest_min_price']) . "元 - " . format_money($carInfo['suggest_max_price']);
        $carInfo['suggest_bid_start_price'] = format_money($carInfo['suggest_bid_start_price']);
        $carInfo['reserve_price'] = format_money($carInfo['reserve_price']);
        //协议详情
        $procedure = $c->detail('car_procedure');

        //加载报告
        $Rep = A('CarReport');
        $report = $Rep->output_report($car_id);

        $this->assign('car_id', $car_id);
        $this->assign('total_car', $count);
        $this->assign('car_id_list', $carIds);
        $this->assign('report', $report);
        $this->assign('car', $carInfo);
        $this->assign('procedure', $procedure);
        $this->assign('is_priced', $is_priced);
        $this->assign('p', $p);
        $this->display();
    }

    //保存定价信息
    public function pricing()
    {
        $car_id = I("post.car_id/d");
        if ((int)$car_id < 1) {
            $this->helpJsonResult(411, "车辆ID错误");
        }
        $c = new Car($car_id);
        $car = $c->detail();
        if ($car['car_status'] >= 5) {//上拍后不能改价
            $this->helpJsonResult(412, "车辆等待上拍，不能改价");
        }

        $suggest_min_price = I("post.min_price", 0);
        $suggest_max_price = I("post.max_price", 0);
        //判断价格是否正确
        if (floatval($suggest_min_price) <= 0) {
            $this->helpJsonResult(400, "请输入正确的最低价格");
        }
        if (floatval($suggest_max_price) <= 0) {
            $this->helpJsonResult(400, "请输入正确的最高价格");
        }
        //数据未修改，不更新
        if ($car['suggest_min_price'] == $suggest_min_price && $car['suggest_max_price'] == $suggest_max_price) {
            $this->helpJsonResult(413, "未做任何修改，无更新");
        }

        $data['suggest_min_price'] = $suggest_min_price;
        $data['suggest_max_price'] = $suggest_max_price;
        $data['price_time'] = date("Y-m-d H:i:s", time());
        $data['price_emp_id'] = $this->emp_id; //定价人
        $result = $c->update($data);
        if (!$result) {
            $this->helpJsonResult(500, "更新失败");
        }

        //记录员工操作日志
        $log = array(
            'emp_id' => $this->user_info['emp_id'],
            'order_id' => 0,
            'car_id' => $car_id,
            'owner_id' => $car['owner_id'],
            'action' => "建议定价 {$suggest_min_price} - {$suggest_max_price}"
        );
        \AuctionCore\System\SystemLog::add($log);

        /**
         * 记录车辆跟踪日志
         */
        if ($car['suggest_max_price'] > 0 && $car['suggest_min_price'] > 0) {
            $car_trace_msg = '修改定价';
        } else {
            $car_trace_msg = '车辆定价';
        }
        $createtime = date("Y-m-d H:i:s", time());
        $car_action_data = array(
            'createtime' => $createtime,
            'action_name' => $car_trace_msg,
            'emp_id' => $this->user_info['emp_id']
        );
        \AuctionCore\CarResource\Car::add_car_trace_log($car_id, $car_action_data);

        $this->helpJsonResult(200, "更新成功");
    }

}
